package com.example.demo.controller;

import com.example.demo.model.Author;
import com.example.demo.model.BlogPost;
import com.example.demo.service.AuthorService;
import com.example.demo.service.BlogPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/api/authors")
public class AuthorController {

    @Autowired
    private  AuthorService authorService;

    @Autowired
    private BlogPostService blogPostService;

    //http://localhost:8080/api/authors/save
    @PostMapping("/save")
    public Author saveAuthor(@RequestBody Author author) {
        System.out.println("Saving author: " + author.getBlogPosts());

        return authorService.saveAuthor(author);
    }

    //http://localhost:8080/api/authors/Macron name should be unique
    @GetMapping("/{name}")
    public Optional<Author> getAuthorByName(@PathVariable String name)
    {
        return authorService.getAuthorByName(name);
    }


//
//    @GetMapping("/{name}/posts")
//    public Iterable<BlogPost> getAuthorBlogPosts(@PathVariable String name){
//        return blogPostService.getBlogPostsByAuthorName(name);
//    }
}
